﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class RoleAuthorityBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public RoleAuthorityBLL()
        {
            DAO = new BaseDAO();
        }
        public IEnumerable<T_RoleAuthority> GetRoleAuthorityList()
        {
            var list = DAO.T_RoleAuthorities.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_RoleAuthority GetRoleAuthorityByID(string ID)
        {
            return DAO.T_RoleAuthorities.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddRoleAuthority(T_RoleAuthority RoleAuthority)
        {
            try
            {
                RoleAuthority.ID = DAO.GetGUID();

                DAO.T_RoleAuthorities.InsertOnSubmit(RoleAuthority);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddRoleAuthority(T_RoleAuthority RoleAuthority, out string RoleAuthorityGuid)
        {
            try
            {
                RoleAuthority.ID = DAO.GetGUID();
                
                DAO.T_RoleAuthorities.InsertOnSubmit(RoleAuthority);
                DAO.SubmitChanges();

                RoleAuthorityGuid = RoleAuthority.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                RoleAuthorityGuid = "";
                return -1;
            }
        }

        public int UpdateRoleAuthority(T_RoleAuthority RoleAuthority)
        {
            try
            {
                T_RoleAuthority updateEntity = DAO.T_RoleAuthorities.SingleOrDefault(t=>t.ID.ToString()==RoleAuthority.ID.ToString());
                if (updateEntity == null)
                {
                    return -1;
                }

                updateEntity.AuthorityID = RoleAuthority.AuthorityID;
                updateEntity.RoleID = RoleAuthority.RoleID;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteRoleAuthority(string ID)
        {
            try
            {
                T_RoleAuthority deleteEntity = DAO.T_RoleAuthorities.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null)
                {
                    return -1;
                }
                DAO.T_RoleAuthorities.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
